<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: table.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter StAND:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class table {
    var $code, $title, $description, $icon, $enabled, $db;

    function __construct() {
		global $order, $db;
		
		$this->code = 'table';
		$this->title = MODULE_SHIPPING_TABLE_TEXT_TITLE;
		$this->description = MODULE_SHIPPING_TABLE_TEXT_DESCRIPTION;
		$this->sort_order = MODULE_SHIPPING_TABLE_SORT_ORDER;
		$this->icon = '';
		$this->tax_class = MODULE_SHIPPING_TABLE_TAX_CLASS;
		$this->enabled = ((MODULE_SHIPPING_TABLE_STATUS == 'True') ? true : false);
		$this->db = $db;
		
		if (($this->enabled == true) && ((int)MODULE_SHIPPING_TABLE_ZONE > 0) && isset($order)) {
			$check_flag = false;
			$check = $this->db->db_query("SELECT zone_id FROM ".TABLE_ZONES_TO_GEO_ZONES." WHERE geo_zone_id = '".MODULE_SHIPPING_TABLE_ZONE."' AND zone_country_id = '".$order->delivery['country']['id']."' ORDER BY zone_id");
			while(!$check->EOF) {
				if ($check->fields['zone_id'] < 1) {
					$check_flag = true;
					break;
				} elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
					$check_flag = true;
					break;
				}
				$check->MoveNext();
			}
			
			if ($check_flag == false)
				$this->enabled = false;
		}
    }

    function quote($method = '') {
    	if(!free_shipping())
			return;
    	
		global $order, $shipping_weight, $shipping_num_boxes, $price;
		
		if (MODULE_SHIPPING_TABLE_MODE == 'price')
			$order_total = $price->RemoveCurr($_SESSION['cart']->show_total());
		else
			$order_total = $shipping_weight;
		
		$shipping = -1;
		$table_cost = preg_split("/[:,]/", MODULE_SHIPPING_TABLE_COST);
		$size = sizeof($table_cost);
		for ($i=0, $n=$size; $i<$n; $i+=2) {
			if ($order_total <= $table_cost[$i]) {
				$shipping = $table_cost[$i+1];
				break;
			}
		}

		if($shipping == -1) {
			$this->enabled = false;
			return;
		}
		
		if (MODULE_SHIPPING_TABLE_MODE == 'weight')
			$shipping = $shipping * $shipping_num_boxes;
		
		$this->quotes = array('id' => $this->code,
								'module' => MODULE_SHIPPING_TABLE_TEXT_TITLE,
								'methods' => array(array('id' => $this->code,
														'title' => MODULE_SHIPPING_TABLE_TEXT_WAY,
														'cost' => $shipping + MODULE_SHIPPING_TABLE_HANDLING)));
		
		if ($this->tax_class > 0)
			$this->quotes['tax'] = get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
		
		if (not_null($this->icon)) 
			$this->quotes['icon'] = image($this->icon, $this->title);
		
		return $this->quotes;
    }

    function check() {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_SHIPPING_TABLE_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
    }

    function install() {
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_SHIPPING_TABLE_STATUS', 'True', '6', '0', 'cfg_SELECT_option(array(\'True\', \'False\'), ', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_TABLE_ALLOWED', '', '6', '0', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_TABLE_COST', '25:8.50,50:5.50,10000:0.00', '6', '0', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_SHIPPING_TABLE_MODE', 'weight', '6', '0', 'cfg_SELECT_option(array(\'weight\', \'price\'), ', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_TABLE_HANDLING', '0', '6', '0', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_SHIPPING_TABLE_TAX_CLASS', '0', '6', '0', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_SHIPPING_TABLE_ZONE', '0', '6', '0', 'get_zone_class_title', 'cfg_pull_down_zone_classes(', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_SHIPPING_TABLE_SORT_ORDER', '0', '6', '0', NOW())");
    }

    function remove() {
		$this->db->db_query("delete FROM ".TABLE_CONFIGURATION." WHERE configuration_key in ('".implode("', '", $this->keys())."')");
    }

    function keys() {
		return array('MODULE_SHIPPING_TABLE_STATUS',
					'MODULE_SHIPPING_TABLE_COST',
					'MODULE_SHIPPING_TABLE_MODE',
					'MODULE_SHIPPING_TABLE_HANDLING',
					'MODULE_SHIPPING_TABLE_ALLOWED',
					'MODULE_SHIPPING_TABLE_TAX_CLASS',
					'MODULE_SHIPPING_TABLE_ZONE',
					'MODULE_SHIPPING_TABLE_SORT_ORDER');
    }
}
?>